package com.appkubes.mall.admin.application.article.controller;


import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.article.entity.vo.ArticleKeyVO;
import com.appkubes.mall.admin.application.article.entity.vo.ArticleVO;
import com.appkubes.mall.admin.application.article.service.ArticleService;
import com.appkubes.mall.admin.application.base.BaseController;
import com.appkubes.mall.admin.config.camel.RequestModel;
import com.appkubes.mall.admin.utils.PageUtil;
import com.appkubes.mall.core.po.MallArticle;
import com.appkubes.mall.core.po.MallArticleWithBLOBs;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api("文章模块")
@RestController
@RequestMapping("/sass_admin/Sarticle")
public class ArticleController extends BaseController {
    @Autowired
    ArticleService articleService;

    @ApiOperation(value = "文章列表")
    @PostMapping("/getList")
    public Response getList(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer limit,
                            @RequestParam(defaultValue = "article_key") String sort,
                            @RequestParam(defaultValue = "desc") String order) {

        PageInfo<MallArticleWithBLOBs> pageinfo = articleService.querySelective(page, limit, sort, order, getAppKey());
        for (MallArticleWithBLOBs model : pageinfo.getList()) {
        }
        return Response.success(new PageUtil<MallArticleWithBLOBs>(pageinfo));
    }

    @ApiOperation(value = "获取文章详情详情")
    @PostMapping("/getArticle")
    public Response getArticle(@RequestParam(defaultValue = "") String articleKey) {
        MallArticle article = articleService.findById(articleKey);
        return Response.success(article);
    }

    @ApiOperation(value = "添加文章")
    @PostMapping("/save")
    public Object save(@RequestModel ArticleVO articleVO) {
        MallArticleWithBLOBs mallArticleWithBLOBs = new MallArticleWithBLOBs();
        BeanUtils.copyProperties(articleVO, mallArticleWithBLOBs);
        int result = 0;
        if (!StringUtils.isEmpty(mallArticleWithBLOBs.getArticleKey())) {
            result = articleService.update(mallArticleWithBLOBs);
        } else {
            result = articleService.add(mallArticleWithBLOBs);
        }
        if (result > 0) {
            return Response.success(result);
        } else {
            return Response.error("保存失败");
        }
    }

    @ApiOperation(value = "修改文章")
    @PostMapping("/edit")
    public Object edit(@RequestModel ArticleKeyVO articleKey) {
        MallArticleWithBLOBs model = articleService.findById(articleKey.getArticleKey());
        return Response.success(model);
    }

    @ApiOperation(value = "删除文章")
    @PostMapping("/delete")
    public Response delete(@RequestParam String ids) {

        int result = articleService.deleteByIds(ids.split(","));
        if (result > 0) {
            return Response.success(result);
        }
        return Response.error("删除失败");

    }

    @ApiOperation(value = "启用文章")
    @PostMapping("/batchenable")
    public Response batchenable(@RequestParam String ids) {
        int ret = articleService.batchenable(ids.split(","));
        if (ret > 0) {
            return Response.success("取消成功");
        } else {
            return Response.error("取消失败");
        }
    }

    @ApiOperation(value = "禁用文章")
    @PostMapping("/batchunenable")
    public Response batchunenable(@RequestParam String ids) {
        int ret = articleService.batchunenable(ids.split(","));
        if (ret > 0) {
            return Response.success("禁用成功");
        } else {
            return Response.error("禁用失败");
        }
    }

}
